import json

from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.

def index(request):
    # 1.参数
    content = "响应对象"
    # 响应内容类型--MIME格式
    # json   application/json
    content_type = 'text/html;charset=utf-8'
    status = 333

    # return HttpResponse(content=content,content_type=content_type,status=status)

    # 2.属性
    response = HttpResponse()
    response.content = '操作属性'
    response.status_code = 444

    # return response

    # 3.子类Jsonresponse
    dict_data = {
        'name':'baoke',
        'age':22,
        'gender':'nan'
    }
    # return HttpResponse(dict_data)   #  浏览器显示结果为  agenamegender

    # 将字典转换为json字符串
    json_data = json.dumps(dict_data)
    # return  HttpResponse(json_data)    # 浏览器显示结果为   {"name": "baoke", "age": 22, "gender": "nan"}

    # content_type = 'application/json' 这句话为下面那句话代码的原理
    # return JsonResponse(dict_data)     # 浏览器显示结果为  {"name": "baoke", "age": 22, "gender": "nan"}

    list_data = [
        {
            'name': '包克',
            'age': 22,
            'gender': 'nan'
        }
    ]

    # 如果向前端传递列表，需要加一个参数safe=Flase
    return JsonResponse(list_data,safe=False)   # 浏览器显示结果为 [{"gender": "nan", "name": "\u5305\u514b", "age": 22}]